#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_TARGET_arg)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/arg.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "arg.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

static void test_base(void)
{
    printf("ARG_MAX %d\r\n", ARG_MAX);

    printf("ARGC %d\r\n", ARGC());
    printf("ARGC %d\r\n", ARGC(A));
    printf("ARGC %d\r\n", ARGC(A, B));
    printf("ARGC %d\r\n", ARGC(A, B, C));

    printf("ARGC %d\r\n", ARGC0());
    printf("ARGC %d\r\n", ARGC0(A));
    printf("ARGC %d\r\n", ARGC0(A, B));
    printf("ARGC %d\r\n", ARGC0(A, B, C));

    printf("ARGS %s\r\n", ARGS(0, "arg0", "arg1", "arg2"));
    printf("ARGS %s\r\n", ARGS(1, "arg0", "arg1", "arg2"));
    printf("ARGS %s\r\n", ARGS(2, "arg0", "arg1", "arg2"));
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: arg [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hv");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("arg version %d.%d.%d\r\n", ARG_V_MAJOR, ARG_V_MINOR, ARG_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_arg)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_arg(void)
{
    command_export("arg", test);

    // command("arg");
}
init_export_app(test_arg);
#endif 
